package main

import (
	"flag"
	"fmt"
	"gitee.com/wayjin/fast-apiservice-playbook/pkg"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cobra"
	"log"
)

var rootCmd = &cobra.Command{}

var (
	serviceConfigFile string
	port int
	address string
	version string
)

func main() {
	flag.StringVar(&serviceConfigFile, "config", "", "service config file")
	flag.IntVar(&port, "port", 9678, "service port")
	//flag.StringVar(&version, "version", "", "")
	flag.Parse()
	args := flag.Args()
	fmt.Println("args:", args)
	config, err := pkg.GetConfigFrom(serviceConfigFile)
	if err != nil {
		log.Fatal(err)
	}

	err = config.ServiceInit(args)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(config)
	router := gin.Default()
	for _, svc := range config.Services.App {
		log.Println("start service ", svc.Name)
		if svc.Method == "POST" {
			router.POST(svc.URI, svc.Execute)
		}
		if svc.Method == "GET" {
			router.GET(svc.URI, svc.Execute)
		}
		if svc.Method == "UPLOAD" {
			router.POST(svc.URI, svc.Upload)
		}
	}
	err = router.Run(fmt.Sprintf(":%d", port))
	if err != nil {
		log.Fatal(err)
	}
}
